package com.example.dell.recyclerview_122.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.dell.recyclerview_122.adapter.MyAdapter;
import com.example.dell.recyclerview_122.R;
import com.example.dell.recyclerview_122.bean.ArticleBean;
import com.example.dell.recyclerview_122.iface.ArticleListener;
import com.example.dell.recyclerview_122.model.ArticleModel;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;//控件
    private RecyclerView.LayoutManager layoutManager;//RecyclerView布局管理
    private MyAdapter myAdapter;//RecyclerView适配器
     private List<ArticleBean> list=null;
    ArticleListener articleListener=new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> beanList) {
            list=beanList;
            myAdapter.setlist(list);
            myAdapter.notifyDataSetChanged();//通知更新
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this,"失败："+msg,Toast.LENGTH_LONG).show();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);

           initRecyclerView();
        ArticleModel model=new ArticleModel();
                model.getResultList("article"
                        ,1
                        ,"rv1l2tidbomann2qn86nln6ur2"
                        ,articleListener);
       }


    private void initRecyclerView() {
      recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
       // list=getData();
        myAdapter=new MyAdapter(this);
        myAdapter.setlist(list);
        recyclerView.setAdapter(myAdapter);


    }

  /* private List<ArticleBean> getData()
    {
        //第一组
       List<ArticleBean>list=new ArrayList<ArticleBean>();
        ArticleBean articleBean;
        articleBean=new ArticleBean();
        articleBean.setArticleId(1);
        articleBean.setTitle("标题1");
        articleBean.setDescription("描述1");
        articleBean.setTime("2017-4-1");
        articleBean.setImgid(R.drawable.f1);
        list.add(articleBean);
        //2
         articleBean=new ArticleBean();
         articleBean.setArticleId(1);
         articleBean.setTitle("标题2");
         articleBean.setDescription("描述2");
         articleBean.setTime("2017-4-2");
         articleBean.setImgid(R.drawable.f2);
         list.add(articleBean);
        //3
       articleBean=new ArticleBean();
       articleBean.setArticleId(3);
       articleBean.setTitle("标题3");
       articleBean.setDescription("描述3");
       articleBean.setTime("2017-4-3");
       articleBean.setImgid(R.drawable.f3);
       list.add(articleBean);
        //4
        articleBean=new ArticleBean();
        articleBean.setArticleId(4);
        articleBean.setTitle("标题4");
        articleBean.setDescription("描述4");
        articleBean.setTime("2017-4-4");
        articleBean.setImgid(R.drawable.f4);
        list.add(articleBean);
        //5
        articleBean=new ArticleBean();
        articleBean.setArticleId(5);
        articleBean.setTitle("标题5");
        articleBean.setDescription("描述5");
        articleBean.setTime("2017-4-5");
        articleBean.setImgid(R.drawable.f5);
        list.add(articleBean);
        //6

       articleBean=new ArticleBean();
       articleBean.setArticleId(6);
       articleBean.setTitle("标题6");
       articleBean.setDescription("描述6");
       articleBean.setTime("2017-4-6");
       articleBean.setImgid(R.drawable.f6);
       list.add(articleBean);
       return list;
    }*/
}
